﻿using IR.WPF.ViewModels.LandingViewModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace IR.WPF.UI.ViewModels
{
    public class VMUserDetail : ViewModelBase
    {
        private string _username;
        private string _fullName;
        private string _email;
        private VMViewPage _parentContext;

        public VMUserDetail(Window boundWindow)
        {
            BoundWindow = boundWindow;
            MainWindow window = App.Current.MainWindow as MainWindow;
            window.PropertyChanged += UserPropertyChanged;
            _parentContext = BoundWindow.DataContext as VMViewPage;
            _parentContext.PropertyChanged += UserPropertyChanged;
        }

        public override void DoCleanup()
        {
            MainWindow window = App.Current.MainWindow as MainWindow;
            window.PropertyChanged -= UserPropertyChanged;
            _parentContext = BoundWindow.DataContext as VMViewPage;
            _parentContext.PropertyChanged -= UserPropertyChanged;
            window.CurrentlySelectedUser = null;
        }

        #region **Properties**
        public string Username
        {
            get { return (_username); }
            private set { _username = value; _parentContext.NotifyPropertyChanged("Username", this); }
        }

        public string FullName
        {
            get { return (_fullName); }
            private set { _fullName = value; _parentContext.NotifyPropertyChanged("FullName", this); }
        }

        public string Email
        {
            get { return (_email); }
            private set { _email = value; _parentContext.NotifyPropertyChanged("Email", this); }
        }
        #endregion

        #region **INotifyProperties**
        public void UserPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "CurrentlySelectedUser")
            {
                MainWindow window = App.Current.MainWindow as MainWindow;
                Username = window.CurrentlySelectedUser.UserController.Username;
                FullName = window.CurrentlySelectedUser.UserController.FName + " "
                    + window.CurrentlySelectedUser.UserController.LName;
                Email = window.CurrentlySelectedUser.UserController.Email;
            }
            else if (e.PropertyName == "ViewPageClosed")
            {
                DoCleanup();
            }
        }
        #endregion
    }
}
